package com.jubao.ejb.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.dao.CouponDaoLocal;
import com.jubao.ejb.dao.CouponDaoRemote;
import com.jubao.ejb.dao.util.DaoConstant;
import com.jubao.ejb.dao.util.DaoUtil;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.Coupon;
import com.jubao.ejb.entity.CouponItem;

@Stateless
@LocalBinding(jndiBinding = DaoConstant.CouponDaoLocalJndiName)
@RemoteBinding(jndiBinding = DaoConstant.CouponDaoRemoteJndiName)
public class CouponDaoImpl implements CouponDaoLocal, CouponDaoRemote {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3264534949922486678L;

	static Logger logger = Logger.getLogger(CouponDaoImpl.class);

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public Coupon findCoupon(long couponSysId) {
		return em.find(Coupon.class, couponSysId);
	}

	public Coupon findCoupon(String couponId) {
		Query q = em.createNamedQuery("CouponDao.eql.findCouponById").setParameter("id", couponId).setMaxResults(1);
		//.setHint("org.hibernate.cacheable", new Boolean(true)).setHint("org.hibernate.cacheRegion", "/entity/Category/Single")
		List<Coupon> couponList = q.getResultList();
		Coupon result = null;
		if (couponList != null && couponList.size() > 0) {
			result = couponList.get(0);
		}
		return result;
	}

	public CouponItem findCouponItem(long couponItemSysId) {
		return em.find(CouponItem.class, couponItemSysId);
	}

	public CouponItem findCouponItem(String couponItemId) {
		Query q = em.createNamedQuery("CouponDao.eql.findCouponItemById").setParameter("id", couponItemId)
				.setMaxResults(1);
		//.setHint("org.hibernate.cacheable", new Boolean(true)).setHint("org.hibernate.cacheRegion", "/entity/Category/Single")
		List<CouponItem> couponItemList = q.getResultList();
		CouponItem result = null;
		if (couponItemList != null && couponItemList.size() > 0) {
			result = couponItemList.get(0);
		}
		return result;
	}

	public long getCouponCount(long storeSysId) {
		Query q = em.createNamedQuery("CouponDao.eql.getCouponCountByStoreSysId")
				.setParameter("storeSysId", storeSysId).setMaxResults(1);
		Long result = (Long) q.getSingleResult();
		return result;
	}

	public List<Coupon> getCouponList(long storeSysId, int pgno, int pgsize) {
		Query q = em.createNamedQuery("CouponDao.eql.getCouponListByStoreSysId").setParameter("storeSysId", storeSysId)
				.setMaxResults(1);
		List<Coupon> coupons = q.setFirstResult(pgno * pgsize).setMaxResults(pgsize).getResultList();
		return coupons;
	}

	public long getCouponItemCount(long storeSysId, long couponSysId, long userSysId) {
		Query q = this.composeGetCouponItemQuery(storeSysId, couponSysId, userSysId, false);
		Long result = (Long) q.getSingleResult();
		return result;
	}

	public List<CouponItem> getCouponItemList(long storeSysId, long couponSysId, long userSysId, int pgno, int pgsize) {
		Query q = this.composeGetCouponItemQuery(storeSysId, couponSysId, userSysId, true);
		List<CouponItem> couponItems = q.setFirstResult(pgno * pgsize).setMaxResults(pgsize).getResultList();
		return couponItems;
	}

	public Coupon saveCoupon(Coupon coupon) {

		return em.merge(coupon);
	}

	public CouponItem saveCouponItem(CouponItem couponItem) {
		CouponItem ci =  em.merge(couponItem);
		em.flush();
		return ci;
	}

	private Query composeGetCouponItemQuery(long storeSysId, long couponSysId, long userSysId, boolean isGetList) {
		String ent = isGetList ? "ci" : "count(ci)";

		String eql = "select " + ent + " from  CouponItem ci";
		String whereand = "";
		if (storeSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  ci.coupon.store.sysId=:storeSysId ";
		}
		if (couponSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  ci.coupon.sysId=:couponSysId ";
		}
		if (userSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  ci.user.sysId=:userSysId ";
		}

		//order by
		eql+=" ORDER BY ci.createdDate DESC";
		
		logger.info("eql=[" + eql + "]");

		Query q = em.createQuery(eql);
		if (storeSysId > -1) {
			q.setParameter("storeSysId", storeSysId);
		}
		if (couponSysId > -1) {
			q.setParameter("couponSysId", couponSysId);
		}
		if (userSysId > -1) {
			q.setParameter("userSysId", userSysId);
		}
		return q;
	}

}
